package de.game.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.util.Log;

public class Util_Date {

	public static Calendar getNowDate(){
		Locale locale = Locale.getDefault();
		//Log.v("Raphael", "Util_Date: Default locale is: "+locale);	
		return Calendar.getInstance(locale);
	}
	
	public static String calToString(Calendar timeCalendar) {
		if (timeCalendar != null) {
			Locale locale = Locale.getDefault();
			//Log.v("Raphael", "Util_Date: Default locale is: "+locale);			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", locale);
			return sdf.format(timeCalendar.getTime());
		} else
			Log.e("Raphael", "The given calendar is null, cannot convert to string.");
			return "";
	}
	
	public static String timeMillisToDateString(long millis){
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millis);
		return calToString(cal);
	}
	
	/**
	 * Only the date, not the time
	 * 
	 * @param timeCalendar
	 * @return
	 */
	public static String calToDateString(Calendar timeCalendar) {
		if (timeCalendar != null) {
			String date = "";
			
			date += timeCalendar.get(Calendar.YEAR) + "-" +
					(timeCalendar.get(Calendar.MONTH) + 1) + "-" +		// +1 !!!
					timeCalendar.get(Calendar.DAY_OF_MONTH);
					
			return date;
		} else
			Log.e("Raphael", "The given calendar is null, cannot convert to string.");
			return "";
	}

	/**
	 * Parses strings in format 2011-01-01T00:00:00 to Calendar objects.
	 * 
	 * @param timeString
	 * @return - the calendar
	 */
	public static Calendar stringToCal(String timeString) {
		Locale locale = Locale.getDefault();
		//Log.v("Raphael", "Default locale is: "+locale);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", locale);
		try {
			Date d = format.parse(timeString);
			Calendar c = new GregorianCalendar();
			c.setTime(d);

			return c;
		} catch (ParseException e) {
			Log.e("Raphael", "Failed parsing the date string.");
			return null;
		}
	}
	
	public static String getMonthName(Calendar cal){
		String[] monthName = {"JAN", "FEB",
				"MAR", "APR", "MAY", "JUN", "JUL",
				"AUG", "SEP", "OCT", "NOV",
				"DEC"};
		String month = monthName[cal.get(Calendar.MONTH)];
		return month;
	}
	
}
